﻿using System;
using System.Collections.Generic;
using System.Text;
using KeaoBase;
using System.Reflection;
using System.Data;

namespace KeaoMDS.AppCode
{
    public class VersionUpdate
    {

        private string m_versionStr = Assembly.GetExecutingAssembly().GetName().Version.ToString();

        public void UpdateVersion()
        {
            if (string.IsNullOrEmpty(KeaoUtils.ConfigGetNodeAttr("软件版本", "版本号")))
            {
                UpdateInfo();
            }
            else
            {
                if (KeaoUtils.ConfigGetNodeAttr("软件版本", "版本号") != m_versionStr)
                {
                    UpdateInfo();
                }
            }
        }

        private void UpdateInfo()
        {
            if (string.IsNullOrEmpty(KeaoUtils.ConfigGetNodeAttr("系统配置", "出库称重超时")))
            {
                KeaoUtils.ConfigSetNodeAttr("系统配置", "出库称重超时", "30");
            }
            if (string.IsNullOrEmpty(KeaoUtils.ConfigGetNodeAttr("系统配置", "出库车辆运输次数预警")))
            {
                KeaoUtils.ConfigSetNodeAttr("系统配置", "出库车辆运输次数预警", "10");
            }
            if (string.IsNullOrEmpty(KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号4")))
            {
                KeaoUtils.ConfigSetNodeAttr("视频监控", "通道号4", "禁用");
            }
            if (string.IsNullOrEmpty(KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号5")))
            {
                KeaoUtils.ConfigSetNodeAttr("视频监控", "通道号5", "禁用");
            }
            if (string.IsNullOrEmpty(KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号6")))
            {
                KeaoUtils.ConfigSetNodeAttr("视频监控", "通道号6", "禁用");
            }
            if (string.IsNullOrEmpty(KeaoUtils.ConfigGetNodeAttr("数据备份", "备份路径")))
            {
                KeaoUtils.ConfigSetNodeAttr("数据备份", "备份路径", "Bak");
            }

            KeaoUtils.ConfigSetNodeAttr("软件版本", "版本号", m_versionStr);
            string execSql = string.Empty;
            string createCondition = KeaoUtils._DB.CreateAndCondition("shengfen", DataValueCommonType.字符串, "云", "", SearchConditionType.等于);
            DataTable Dt1 = KeaoUtils._DB.GetDataTableByCondition("tbl_shengfen", createCondition);
            if (Dt1.Rows.Count == 0)
            {
                TableRow newRow1 = new TableRow("tbl_shengfen", "031", DBCtl.插入);
                newRow1.DataCols.Add(new TableCol() { ColName = "shengfen", ValueMember = "云", ValueType = DataValueCommonType.字符串 });
                newRow1.DataCols.Add(new TableCol() { ColName = "shouzimu", ValueMember = "Y", ValueType = DataValueCommonType.字符串 });
                KeaoUtils._DB.SaveTableRows(newRow1);
            }
            createCondition = KeaoUtils._DB.CreateAndCondition("name", DataValueCommonType.字符串, "tbl_tuiku", "", SearchConditionType.等于);
            DataTable Dt2 = KeaoUtils._DB.GetDataTableByCondition("sqlite_master", createCondition);
            if (Dt2.Rows.Count == 0)
            {
                execSql = @"CREATE TABLE [tbl_tuiku](
                               [kehubianhao] varchar(30)
                                ,[bangfang] varchar(50)
                                ,[zhandian] varchar(50)
                                ,[tuikubeizhu] varchar(200)
                                ,[shangchuanzhuangtai] varchar(50)
                                ,[tuikushijian] datetime
                                ,[caozuoyuan] varchar(50)
                                ,[tuikubiaoduan] varchar(100)
                                ,[tuikujingzhong] real
                                ,[tuikupizhong] real
                                ,[tuikumaozhong] real
                                ,[tuikubianhao] varchar(50)
                                ,[tuikuchepaihao] varchar(50)
                                ,[tuikudanwei] varchar(50)
                                ,[tuikuxinghao] varchar(50)
                                ,[tuikuwuliao] varchar(50)
                                ,[tuikuleixing] varchar(50)
                                ,[id] varchar(50) PRIMARY KEY
                                    )";
                KeaoUtils._DB.ExecuteSql(execSql);
            }
            createCondition = KeaoUtils._DB.CreateAndCondition("quanxianmingcheng", DataValueCommonType.字符串, "退库操作", "", SearchConditionType.等于);
            DataTable Dt3 = KeaoUtils._DB.GetDataTableByCondition("tbl_quanxian", createCondition);
            if (Dt3.Rows.Count == 0)
            {
                TableRow newRow1 = new TableRow("tbl_quanxian","018", DBCtl.插入);
                newRow1.DataCols.Add(new TableCol() { ColName="quanxianmingcheng", ValueMember="退库操作", ValueType= DataValueCommonType.字符串 });
                KeaoUtils._DB.SaveTableRows(newRow1);
            }

            execSql = string.Format("pragma table_info(tbl_biaoduan)");
            DataTable Dt4 = KeaoUtils._DB.ExecuteTable(execSql).Tables[0];
            bool hasBiaoDuanHouDu = false;
            bool hasZuoYouFu = false;
            bool hasGongLiHao1 = false;
            bool hasGongLiHao2 = false;
            bool hasRiQi = false;
            bool hasFangLiang = false;
            bool hasGanMiDu = false;
            bool hasJiHuaLiang = false;
            bool hasGongCheng = false;
            foreach (DataRow row in Dt4.Rows)
            {
                if (row["name"].Equals("biaoduanhoudu"))
                {
                    hasBiaoDuanHouDu = true;
                }
                if (row["name"].Equals("zuoyoufu"))
                {
                    hasZuoYouFu = true;
                }
                if (row["name"].Equals("gonglihao1"))
                {
                    hasGongLiHao1 = true;
                }
                if (row["name"].Equals("gonglihao2"))
                {
                    hasGongLiHao2 = true;
                }
                if (row["name"].Equals("riqi"))
                {
                    hasRiQi = true;
                }
                if (row["name"].Equals("fangliang"))
                {
                    hasFangLiang = true;
                }
                if (row["name"].Equals("ganmidu"))
                {
                    hasGanMiDu = true;
                }
                if (row["name"].Equals("jihualiang"))
                {
                    hasJiHuaLiang = true;
                }
                if (row["name"].Equals("gongcheng"))
                {
                    hasGongCheng = true;
                }
            }
            if (!hasBiaoDuanHouDu)
            {
                execSql = string.Format("alter table tbl_biaoduan add column biaoduanhoudu real");
                KeaoUtils._DB.ExecuteSql(execSql);
            }
            if (!hasZuoYouFu)
            {
                execSql = string.Format("alter table tbl_biaoduan add column zuoyoufu varchar(50)");
                KeaoUtils._DB.ExecuteSql(execSql);
            }
            if (!hasGongLiHao1)
            {
                execSql = string.Format("alter table tbl_biaoduan add column gonglihao1 real");
                KeaoUtils._DB.ExecuteSql(execSql);
            }
            if (!hasGongLiHao2)
            {
                execSql = string.Format("alter table tbl_biaoduan add column gonglihao2 real");
                KeaoUtils._DB.ExecuteSql(execSql);
            }
            if (!hasRiQi)
            {
                execSql = string.Format("alter table tbl_biaoduan add column riqi datetime");
                KeaoUtils._DB.ExecuteSql(execSql);
            }
            if (!hasFangLiang)
            {
                execSql = string.Format("alter table tbl_biaoduan add column fangliang real");
                KeaoUtils._DB.ExecuteSql(execSql);
            }
            if (!hasGanMiDu)
            {
                execSql = string.Format("alter table tbl_biaoduan add column ganmidu real");
                KeaoUtils._DB.ExecuteSql(execSql);
            }
            if (!hasJiHuaLiang)
            {
                execSql = string.Format("alter table tbl_biaoduan add column jihualiang real");
                KeaoUtils._DB.ExecuteSql(execSql);
            }
            if (!hasGongCheng)
            {
                execSql = string.Format("alter table tbl_biaoduan add column gongcheng varchar(50)");
                KeaoUtils._DB.ExecuteSql(execSql);
            }

            createCondition = KeaoUtils._DB.CreateAndCondition("quanxianmingcheng", DataValueCommonType.字符串, "基础数据配置", "", SearchConditionType.等于);
            DataTable Dt5 = KeaoUtils._DB.GetDataTableByCondition("tbl_quanxian", createCondition);
            if (Dt5.Rows.Count == 0)
            {
                TableRow newRow1 = new TableRow("tbl_quanxian", "019", DBCtl.插入);
                newRow1.DataCols.Add(new TableCol() { ColName = "quanxianmingcheng", ValueMember = "基础数据配置", ValueType = DataValueCommonType.字符串 });
                KeaoUtils._DB.SaveTableRows(newRow1);
            }

            execSql = string.Format("pragma table_info(tbl_xinghao)");
            DataTable Dt6 = KeaoUtils._DB.ExecuteTable(execSql).Tables[0];
            bool hasShuiWenLiao = false;
            bool hasHunNingTu = false;
            bool hasLiQingLIao = false;
            foreach (DataRow row in Dt6.Rows)
            {
                if (row["name"].Equals("shuiwenliao"))
                {
                    hasShuiWenLiao = true;
                }
                if (row["name"].Equals("hunningtu"))
                {
                    hasHunNingTu = true;
                }
                if (row["name"].Equals("liqingliao"))
                {
                    hasLiQingLIao = true;
                }
            }
            if (!hasShuiWenLiao)
            {
                execSql = string.Format("alter table tbl_xinghao add column shuiwenliao varchar(50)");
                KeaoUtils._DB.ExecuteSql(execSql);
            }
            if (!hasHunNingTu)
            {
                execSql = string.Format("alter table tbl_xinghao add column hunningtu varchar(50)");
                KeaoUtils._DB.ExecuteSql(execSql);
            }
            if (!hasLiQingLIao)
            {
                execSql = string.Format("alter table tbl_xinghao add column liqingliao varchar(50)");
                KeaoUtils._DB.ExecuteSql(execSql);
            }

            createCondition = KeaoUtils._DB.CreateAndCondition("name", DataValueCommonType.字符串, "tbl_peihebi", "", SearchConditionType.等于);
            DataTable Dt7 = KeaoUtils._DB.GetDataTableByCondition("sqlite_master", createCondition);
            if (Dt7.Rows.Count == 0)
            {
                execSql = @"Create table tbl_peihebi(
                            [shangchuanzhuangtai] varchar(50)
                            ,[kehubianhao] varchar(50)
                            ,[zhandian] varchar(50)
                            ,[peibi] real
                            ,[guanlianbianhao] varchar(50)
                            ,[guanlianwuliao] varchar(50)
                            ,[peibiwuliao] varchar(50)
                            ,[peibileixing] varchar(50)
                            ,[gongcheng] varchar(50)
                            ,[peibiriqi] datetime
                            ,[id] varchar(50) PRIMARY KEY
                               ) ";
                KeaoUtils._DB.ExecuteSql(execSql);
            }

            createCondition = KeaoUtils._DB.CreateAndCondition("name", DataValueCommonType.字符串, "tbl_hetong", "", SearchConditionType.等于);
            DataTable Dt8 = KeaoUtils._DB.GetDataTableByCondition("sqlite_master", createCondition);
            if (Dt8.Rows.Count == 0)
            {
                execSql = @"Create  TABLE tbl_hetong(
                            [shangchuanzhuangtai] varchar(50)
                            ,[kehubianhao] varchar(50)
                            ,[zhandian] varchar(50)
                            ,[hetongzhuangtai] varchar(50)
                            ,[jieshushijian] datetime
                            ,[kaishishijian] datetime
                            ,[wuliaomingcheng] varchar(50)
                            ,[wuliao] varchar(50)
                            ,[hetongleixing] varchar(50)
                            ,[danweimingcheng] varchar(50)
                            ,[danwei] varchar(50)
                            ,[hetongzhongliang] numeric(10,2)
                            ,[hetongbianhao] varchar(50)
                            ,[id] varchar(50) PRIMARY KEY
                               )";
                KeaoUtils._DB.ExecuteSql(execSql);
            }

            createCondition = KeaoUtils._DB.CreateAndCondition("name", DataValueCommonType.字符串, "tbl_peibidan", "", SearchConditionType.等于);
            DataTable Dt9 = KeaoUtils._DB.GetDataTableByCondition("sqlite_master", createCondition);
            if (Dt9.Rows.Count == 0)
            {
                execSql = @"Create  TABLE tbl_peibidan(
                            [shangchuanzhuangtai] varchar(50)
                            ,[kehubianhao] varchar(50)
                            ,[zhandian] varchar(50)
                            ,[peibi] real
                            ,[guanlianbianhao] varchar(50)
                            ,[guanlianwuliao] varchar(50)
                            ,[peibiwuliao] varchar(50)
                            ,[leixing] varchar(50)
                            ,[mingcheng] varchar(50)
                            ,[id] varchar(50) PRIMARY KEY
                               ) ";
                KeaoUtils._DB.ExecuteSql(execSql);
            }

            createCondition = KeaoUtils._DB.CreateAndCondition("name", DataValueCommonType.字符串, "tbl_zidian", "", SearchConditionType.等于);
            DataTable Dt10 = KeaoUtils._DB.GetDataTableByCondition("sqlite_master", createCondition);
            if (Dt10.Rows.Count == 0)
            {
                execSql = @"Create  TABLE tbl_zidian(
                            [kehubianhao] varchar(50)
                            ,[zhandian] varchar(50)
                            ,[shangchuanzhuangtai] varchar(50)
                            ,[shijian] datetime
                            ,[xiangmu] varchar(50)
                            ,[fenzu] varchar(50)
                            ,[id] varchar(50) PRIMARY KEY
                               ) ";
                KeaoUtils._DB.ExecuteSql(execSql);
            }

            execSql = string.Format("pragma table_info(tbl_danwei)");
            DataTable Dt11 = KeaoUtils._DB.ExecuteTable(execSql).Tables[0];
            bool hasGongYingShang = false;
            bool hasGongYingShangMingCheng = false;
            foreach (DataRow row in Dt11.Rows)
            {
                if (row["name"].Equals("gongyingshang"))
                {
                    hasGongYingShang = true;
                }
                if (row["name"].Equals("gongyingshangmingcheng"))
                {
                    hasGongYingShangMingCheng = true;
                }
            }
            if (!hasGongYingShang)
            {
                execSql = string.Format("alter table tbl_danwei add column gongyingshang varchar(50)");
                KeaoUtils._DB.ExecuteSql(execSql);
            }
            if (!hasGongYingShangMingCheng)
            {
                execSql = string.Format("alter table tbl_danwei add column gongyingshangmingcheng varchar(50)");
                KeaoUtils._DB.ExecuteSql(execSql);
            }

            createCondition = KeaoUtils._DB.CreateAndCondition("name", DataValueCommonType.字符串, "tbl_gongyingshang", "", SearchConditionType.等于);
            DataTable Dt12 = KeaoUtils._DB.GetDataTableByCondition("sqlite_master", createCondition);
            if (Dt12.Rows.Count == 0)
            {
                execSql = @"Create  TABLE tbl_gongyingshang(
                            [shangchuanzhuangtai] varchar(50)
                            ,[kehubianhao] varchar(50)
                            ,[zhandian] varchar(50)
                            ,[zhuangtai] varchar(50)
                            ,[mingcheng] varchar(50)
                            ,[bianhao] varchar(50)
                            ,[id] varchar(50) PRIMARY KEY
                               ) ";
                KeaoUtils._DB.ExecuteSql(execSql);
            }

            createCondition = KeaoUtils._DB.CreateAndCondition("fenzu", DataValueCommonType.字符串, "水稳料", "", SearchConditionType.等于);
            DataTable Dt13 = KeaoUtils._DB.GetDataTableByCondition("tbl_zidian", createCondition);
            if (Dt13.Rows.Count == 0)
            {
                List<string> sqls = new List<string>();
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "水稳料", "粗集料1", DateTime.Now.ToString("s")));
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "水稳料", "粗集料2", DateTime.Now.ToString("s")));
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "水稳料", "粗集料3", DateTime.Now.ToString("s")));
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "水稳料", "粗集料4", DateTime.Now.ToString("s")));
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "水稳料", "粗集料5", DateTime.Now.ToString("s")));
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "水稳料", "细集料1", DateTime.Now.ToString("s")));
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "水稳料", "细集料2", DateTime.Now.ToString("s")));
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "水稳料", "细集料3", DateTime.Now.ToString("s")));
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "水稳料", "细集料4", DateTime.Now.ToString("s")));
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "水稳料", "细集料5", DateTime.Now.ToString("s")));
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "水稳料", "粉料1", DateTime.Now.ToString("s")));
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "水稳料", "粉料2", DateTime.Now.ToString("s")));
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "水稳料", "粉料3", DateTime.Now.ToString("s")));
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "水稳料", "水", DateTime.Now.ToString("s")));
                KeaoUtils._DB.ExecuteSqls(sqls);
            }
            createCondition = KeaoUtils._DB.CreateAndCondition("fenzu", DataValueCommonType.字符串, "路基", "", SearchConditionType.等于);
            DataTable Dt14 = KeaoUtils._DB.GetDataTableByCondition("tbl_zidian", createCondition);
            if (Dt14.Rows.Count == 0)
            {
                List<string> sqls = new List<string>();
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "路基", "基层", DateTime.Now.ToString("s")));
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "路基", "底基层", DateTime.Now.ToString("s")));
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "路基", "垫层", DateTime.Now.ToString("s")));
                KeaoUtils._DB.ExecuteSqls(sqls);
            }
            createCondition = KeaoUtils._DB.CreateAndCondition("fenzu", DataValueCommonType.字符串, "月结", "", SearchConditionType.等于);
            DataTable Dt15 = KeaoUtils._DB.GetDataTableByCondition("tbl_zidian", createCondition);
            if (Dt15.Rows.Count == 0)
            {
                List<string> sqls = new List<string>();
                sqls.Add(string.Format("insert into tbl_zidian(id,fenzu,xiangmu,shijian,shangchuanzhuangtai,zhandian,kehubianhao) values ('{0}','{1}','{2}','{3}','0','','')", Guid.NewGuid().ToString(), "月结", "15", DateTime.Now.ToString("s")));
                KeaoUtils._DB.ExecuteSqls(sqls);
            }


        }
    }
}
